//
//  BCUserOverdueManager.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/11.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCUserOverdueManager.h"

//
#import "BCTimerHandler.h"


//用户进入后台时间太长后发出通知 注销登录用户信息
NSString * const BCUserEnterBackgroundLongTimeNotification = @"userEnterBackgroundLongTimeNotification";

@interface BCUserOverdueManager () <BCTimerHandlerDelegate>

@property (nonatomic, strong) BCTimerHandler *timerHandler;

@end

@implementation BCUserOverdueManager

- (BCTimerHandler *)timerHandler {
    if (!_timerHandler) {
        _timerHandler = [[BCTimerHandler alloc] init];
        _timerHandler.timeInterval = 10.0;
        _timerHandler.delegate = self;
    }
    return _timerHandler;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
    }
    return self;
}

/**
 开始监听用户登录有效期
 */
- (void)startMonitoring {
    [self.timerHandler start];
}

/**
 停止监听用户登录有效期
 */
- (void)stopMonitoring {
    if (_timerHandler) {
        [_timerHandler stop];
        _timerHandler = nil;
    }
}



/**
 获取登录用户过期时间 nil说明没有登录 或者登录有效期时间出错
 */
- (NSDate *)currentLoginUserOverdue {
    UserInfoModel *userInfo = [BCUserManager sharedUser].userInfo;
    if (userInfo) {
        NSTimeInterval overdue = userInfo.exp;
        return [NSDate dateWithTimeIntervalSince1970:overdue];
    }
    return nil;
}



#pragma mark - BCTimerHandlerDelegate
- (void)timerHandlerRepeatsAction {
    //应用当前运行状态
    UIApplicationState applicationState = [UIApplication sharedApplication].applicationState;
    switch (applicationState) {
        case UIApplicationStateActive:
        {
            NSLog(@"\n\n应用当前活跃中...\n");
        }
            break;
        case UIApplicationStateInactive:
        {
            NSLog(@"\n\n应用即将活跃中...\n");
        }
            break;
        case UIApplicationStateBackground:
        {
            NSLog(@"\n\n应用在后台运行...\n");
        }
            break;
        default:
            break;
    }
    //判断是否登录
    if ([BCUserManager sharedUser].isLogin) {
        //如果即将过期且状态是活跃是需要更新JWT
        
        NSDate *overdueDate = [self currentLoginUserOverdue];
        NSLog(@"\n当前用户登录有效期:%@\n",[overdueDate stringWithFormat:@"yyyy-MM-dd HH:mm:ss"]);
    }else {
        NSLog(@"\n当前没有用户登录!!!\n");
    }
}

#pragma mark - applicationDidEnterBackground:
- (void)applicationDidEnterBackground:(NSNotification *)noti {
    self.enterBackgroundDate = [NSDate date];
}
- (void)applicationDidBecomeActive:(NSNotification *)noti {
//    if (self.enterBackgroundDate) {
//        NSDate *enterBackgroundDate = [self.enterBackgroundDate copy];
//        self.enterBackgroundDate = nil;
//        NSDate *activeDate = [NSDate date];
//        NSTimeInterval seconds = [activeDate timeIntervalSinceDate:enterBackgroundDate];//退出到后台多久
//        if ([BCUserManager sharedUser].isLogin && seconds>5*60) {//先设定5分钟
//            [[NSNotificationCenter defaultCenter] postNotificationName:BCUserEnterBackgroundLongTimeNotification object:nil];
//        }
//    }
}
- (void)applicationWillEnterForeground:(NSNotification *)noti {
    if (self.enterBackgroundDate) {
        NSDate *enterBackgroundDate = [self.enterBackgroundDate copy];
        self.enterBackgroundDate = nil;
        NSDate *activeDate = [NSDate date];
        NSTimeInterval seconds = [activeDate timeIntervalSinceDate:enterBackgroundDate];//退出到后台多久
        if ([BCUserManager sharedUser].isLogin && seconds>5*60) {//先设定5分钟
            [[BCUserManager sharedUser] clearLoginUser];
            [[NSNotificationCenter defaultCenter] postNotificationName:BCUserEnterBackgroundLongTimeNotification object:nil];
        }
    }
}


- (void)dealloc {
    if (_timerHandler) {
        [_timerHandler stop];
        _timerHandler = nil;
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
